﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cinchoo.Core;
using System.IO;
using System.ComponentModel;
using ChoWorkflowServer.Model;
using Cinchoo.Core.Windows;

namespace ChoWorkflowDashboard
{
    /// <summary>
    /// Interaction logic for ChoTaskCmdParamsEditor.xaml
    /// </summary>
    [Serializable]
    public partial class ChoTaskCmdParamsEditor : MetroWindow, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private readonly string _workflowName;

        private string _cmdParams;
        public string CmdParams
        {
            get { return _cmdParams; }
            set
            {
                if (_cmdParams == value) return;

                _cmdParams = value;
                RaisePropertyChangedEvent("CmdParams");
            }
        }

        private IChoPropertyGridSourceObject _cmdParamsProperties;
        public IChoPropertyGridSourceObject CmdParamsProperties
        {
            get { return _cmdParamsProperties; }
            set
            {
                if (_cmdParamsProperties == value) return;

                _cmdParamsProperties = value;
                RaisePropertyChangedEvent("CmdParamsProperties");

            }
        }

        private string _statusMsg = "Ready";
        public string StatusMsg
        {
            get { return _statusMsg; }
            set
            {
                if (_statusMsg == value) return;

                _statusMsg = value;
                RaisePropertyChangedEvent("StatusMsg");

            }
        }

        public ChoTaskCmdParamsEditor(string wfName, string cmdParams)
        {
            ChoGuard.ArgumentNotNullOrEmpty(wfName, "WorkflowName");
            _workflowName = wfName;
            CmdParams = cmdParams;

            InitializeComponent();
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //pgSettings.Visibility = Visibility.Visible;
                //txtSettings.Visibility = Visibility.Collapsed;

                CmdParamsProperties = NewPropertyGridSourceObject(_workflowName);

                if (CmdParamsProperties == null)
                {
                    dpMain.Children.Remove(pgSettings); //.Visibility = Visibility.Collapsed;
                    //txtSettings.Visibility = Visibility.Visible;
                }
                else
                {
                    CmdParamsProperties.SetParams(CmdParams);
                    dpMain.Children.Remove(txtSettings); //.Visibility = Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                dpMain.Children.Remove(pgSettings); //.Visibility = Visibility.Collapsed;
                StatusMsg = "Error loading plugin. {0}.".FormatString(ex.Message);
            }

            this.DataContext = this;
        }

        private IChoPropertyGridSourceObject NewPropertyGridSourceObject(string wfName)
        {
            if (wfName.IsNullOrWhiteSpace()) return null;

            string pgSourceObjectName = "{0}_PGSourceObject".FormatString(wfName);

            foreach (string piFilePath in Directory.GetFiles(ChoWFPlugInsFolderSynchronizer.Value.BaseDir, "*.dll"))
            {
                Assembly assembly = Assembly.Load(File.ReadAllBytes(piFilePath));
                var x = (from t in assembly.GetTypes()
                         where typeof(IChoPropertyGridSourceObject).IsAssignableFrom(t) && String.Compare(t.Name, pgSourceObjectName, true) == 0
                         select t).FirstOrDefault();
                if (x != null)
                    return ChoActivator.CreateInstance(x) as IChoPropertyGridSourceObject;
            }

            return null;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            CmdParams = null;
            Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            CmdParams = CmdParamsProperties != null ? CmdParamsProperties.GetParams() : CmdParams;
            Close();
        }

        private void RaisePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
